package ch4.SqString;

public class SqStringClass {    //顺序串类
    final int MaxSize = 100;

    //完成：定义存放串中字符的数组
    char[] data;    //存放串中字符
    int size;   //串中字符个数

    public SqStringClass() {    //构造方法
        data = new char[MaxSize];
        size = 0;
    }

    //串的基本运算算法
    public void StrAssign(char[] cstr) {    // 创建一个串
        // 完成：将cstr中的字符赋给data
        for (int i = 0; i < cstr.length; i++) {
            data[i] = cstr[i];
        }

        size = cstr.length;
    }

    public char geti(int i) {
        return this.data[i];
    }

    public SqStringClass StrCopy() {    //串复制
        // 完成：将data赋给一个新的SqStringClass顺序串对象，并返回该顺序串
        SqStringClass s = new SqStringClass();
        for (int i = 0; i < size; i++) {
            s.data[i] = data[i];
        }
        s.size = size;
        return s;
    }

    public int size() {    //求串长
        return size;
    }

    public SqStringClass Concat(SqStringClass t) {  //串连接
        SqStringClass s = new SqStringClass();      //新建一个空串
        s.size = this.size + t.size;
        for (int i = 0; i < size; i++) {    //将当前串data[0..str.size-1]→s
            s.data[i] = this.data[i];
        }
        //for (int i = 0; i < t.size; i++) {  //将t.data[0..t.size-1]→s
        //    s.data[this.size + i] = t.data[i];
        //}
        for (int i = this.size; i < s.size; i++) {
            s.data[i] = t.data[i-this.size];
        }
        return s;   //返回新串s
    }

    // 求子串
    public SqStringClass SubStr(int i, int j) {     //求子串
        SqStringClass s = new SqStringClass();      //新建一个空串
        if (i < 0 || i >= size || j < 0 || i + j > size) {
            return s;   //参数不正确时返回空串
        }
        for (int k = i; k <= i + j; k++) {    //将data[i..i+j-1]→s
            s.data[k - i] = data[k];
        }
        s.size = j;
        return s;   //返回新建的顺序串
    }

    // 串插入
    public SqStringClass InsStr(int i, SqStringClass t) {
        SqStringClass s = new SqStringClass();    //新建一个空串
        if (i < 0 || i > size) {    //参数不正确时返回空串
            return s;
        }
        for (int j = 0; j < i; j++) {        //将当前串data[0..i-2]→s
            s.data[j] = data[j];
        }
        for (int j = 0; j < t.size; j++) {    //将t.data[0..t.size-1]→s
            s.data[i + j] = t.data[j];
        }
        for (int j = i; j < size; j++) {    //将当前串data[i-1..size-1]→s
            s.data[t.size + j] = data[j];
        }
        s.size = size + t.size;
        return s;   //返回新建的顺序串
    }

    //串删除
    public SqStringClass DelStr(int i, int j) {
        SqStringClass s = new SqStringClass();      //新建一个空串
        if (i < 0 || i > size || i + j > size) {    //参数不正确时返回空串
            return s;
        }
        for (int k = 0; k < i; k++) {   //将当前串data[0..i-2]ès
            s.data[k] = data[k];
        }
        for (int k = i + j; k < size; k++) {    //将当前串data[i+j-1..size-1]→s
            s.data[k - j] = data[k];
        }
        s.size = size - j;
        return s;   //返回新建的顺序串
    }

    //串替换
    public SqStringClass RepStr(int i, int j, SqStringClass t) {
        SqStringClass s = new SqStringClass();      //新建一个空串
        if (i < 0 || i > size || i + j > size) {    //参数不正确时返回空串
            return s;
        }
        for (int k = 0; k < i; k++) {           //将当前串data[0..i-2]ès
            s.data[k] = data[k];
        }
        for (int k = 0; k < t.size; k++) {      //将s.data[0..s.size-1]→s
            s.data[i + k] = t.data[k];
        }
        for (int k = i + j; k < size; k++) {    //将当前串data[i+j-1..size-1]→s
            s.data[t.size + k - j] = data[k];
        }
        s.size = size - j + t.size;
        return s;   //返回新建的顺序串
    }

    @Override
    public String toString() {    //将串转换为字符串。
        String ans = "";
        for (int i = 0; i < size; i++) {
            ans += data[i];
        }
        return ans;
    }

    public static void main(String[] args) {
        char[] cstr1 = {'a', 'b', 'c', 'd'};
        // 完成：创建一个由cstr1元素构成的顺序串s1，并输出s1以及长度
        SqStringClass s1 = new SqStringClass();
        s1.StrAssign(cstr1);
        System.out.println("s1: " + s1);
        System.out.println("s1的长度: " + s1.size());

        char[] cstr2 = {'1', '2', '3'};
        SqStringClass s2 = new SqStringClass();
        s2.StrAssign(cstr2);
        System.out.println("s2: " + s2);
        System.out.println("s2的长度: " + s2.size());

        // 完成：将s1拷贝给顺序串s3，并输出s3以及长度
        System.out.println("s1=>s3");
        SqStringClass s3 = s1.StrCopy();
        System.out.println("s3: " + s3);

        // 完成：将s1和s2连接起来创建一个新的顺序串s4，并输出s4
        System.out.println("s1和s2连接=>s4");
        SqStringClass s4 = s1.Concat(s2);
        System.out.println("s4: " + s4);

        System.out.println("s4[2..5]=>s5");
        SqStringClass s5 = s4.SubStr(2, 5);
        System.out.println("s5: " + s5);

        // 完成：将s2插入到s4中序号2的位置
        System.out.println("s4中序号2位置插入s2=>s6");
        SqStringClass s6 = s4.InsStr(2, s2);
        System.out.println("s6: " + s6);

        System.out.println("s6中删除[2,3]=>s7");
        SqStringClass s7 = s6.DelStr(2, 3);
        System.out.println("s7: " + s7);

        System.out.println("s6中[2,3]替换为s1=>s8");
        SqStringClass s8 = s6.RepStr(2, 3, s1);
        System.out.println("s8: " + s8);

		//s5=s3.SubStr(2,4);
		//System.out.println("s5:" + s5.DispStr();

		//System.out.println("从s3中删除s3[3..6]字符=>s6"）;
		//s6=s3.DelStr(3,4);
		//System.out.println("s6:" + s6.DispStr();

		//System.out.println("将s4[2..4]替换成s1=>s7"）;
		//s7=s4.RepStr(2,3,s1);
		//System.out.println("s7:" + s7.DispStr();

		//System.out.println("main函数结束,销毁所有的串对象"）;
    }
}
